<template>
  <div class="home-wrap">
    <div class="home-header">
      <card :item="item" v-for="item in cardData" :key="item.title"></card>
    </div>
    <div class="home-echarts">
      <div class="my-echart" ref="chartRef"></div>
    </div>
  </div>
</template>

<script>
import { orderTotaldata } from "@/api/order";
import Card from "./card.vue";
//引入echarts
import * as echarts from "echarts";
export default {
  components: {
    Card,
  },
  data() {
    return {
      cardData: [],
    };
  },
  methods: {
    async getDetail() {
      let res = await orderTotaldata();
      console.log(res);
      let {
        xData,
        orderData,
        amountData,
        totalOrder,
        totalAmount,
        todayOrder,
        totayAmount,
      } = res.data;
      let temp = [
        {
          title: "总订单",
          count: totalOrder,
          imgUrl: require("@/assets/img/订单.png"),
        },
        {
          title: "总销售额",
          count: totalAmount,
          imgUrl: require("@/assets/img/钱.png"),
        },
        {
          title: "今日订单数",
          count: todayOrder,
          imgUrl: require("@/assets/img/dingdan.png"),
        },
        {
          title: "今日销售额",
          count: totayAmount,
          imgUrl: require("@/assets/img/人民币.png"),
        },
      ];
      this.cardData = temp;
      //处理数据
      let series = [
        {
          type: "line",
          name: "金额数据",
          data: amountData,
        },
        {
          type: "line",
          name: "订单数据",
          data: orderData,
        },
      ];
      //画图
      this.renderLine({
        series,
        xData,
      });
    },
    renderLine(obj) {
      let myChart = echarts.init(this.$refs.chartRef);
      //配置项
      let options = {
        title: {
          text: "数据统计",
        },
        tooltip: {
          trigger: "axis",
        },
        legend: {},
        grid: {
          left: "3%",
          right: "4%",
          bottom: "3%",
          containLabel: true,
        },
        toolbox: {
          feature: {
            saveAsImage: {},
          },
        },
        xAxis: {
          type: "category",
          boundaryGap: false,
          data: obj.xData,
        },
        yAxis: {
          type: "value",
        },
        series: obj.series,
      };
      // 绘制图表
      myChart.setOption(options);
    },
  },
  created() {
    this.getDetail();
  },
};
</script>

<style lang="scss" scoped>
.home-wrap {
  padding: 0 20px;
}
.home-header {
  display: flex;
  justify-content: space-between;
  margin-top: 40px;
}
.home-echarts {
  background-color: #fff;
  margin-top: 30px;
}
.my-echart {
  width: 100%;
  height: 400px;
  margin-top: 100px;
}
</style>